/**
 */
package org.mro.grib.model.files.secondEdition.sections.impl;

import java.nio.ByteBuffer;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.mro.grib.core.DataRepresentationException;
import org.mro.grib.core.SectionException;
import org.mro.grib.model.files.secondEdition.dataRepresentations.DataRepresentationTemplateNumber;
import org.mro.grib.model.files.secondEdition.dataRepresentations.IDataRepresentation;
import org.mro.grib.model.files.secondEdition.sections.DataRepresentationSection;
import org.mro.grib.model.files.secondEdition.sections.SectionsPackage;
import org.mro.grib.util.GribSecondEditionTemplatesFactory;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Data Representation Section</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.DataRepresentationSectionImpl#getStartOffset <em>Start Offset</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.DataRepresentationSectionImpl#getEndOffset <em>End Offset</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.DataRepresentationSectionImpl#getLength <em>Length</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.DataRepresentationSectionImpl#getTotalNumberOfDataPoints <em>Total Number Of Data Points</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.DataRepresentationSectionImpl#getDataRepresentationTemplateNumber <em>Data Representation Template Number</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.DataRepresentationSectionImpl#getDataRepresentationTemplate <em>Data Representation Template</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class DataRepresentationSectionImpl extends EObjectImpl implements DataRepresentationSection {
	/**
	 * The default value of the '{@link #getStartOffset() <em>Start Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartOffset()
	 * @generated
	 * @ordered
	 */
	protected static final long START_OFFSET_EDEFAULT = 0L;

	/**
	 * The cached value of the '{@link #getStartOffset() <em>Start Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartOffset()
	 * @generated
	 * @ordered
	 */
	protected long startOffset = START_OFFSET_EDEFAULT;

	/**
	 * The default value of the '{@link #getEndOffset() <em>End Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getEndOffset()
	 * @generated
	 * @ordered
	 */
	protected static final long END_OFFSET_EDEFAULT = 0L;

	/**
	 * The cached value of the '{@link #getEndOffset() <em>End Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getEndOffset()
	 * @generated
	 * @ordered
	 */
	protected long endOffset = END_OFFSET_EDEFAULT;

	/**
	 * The default value of the '{@link #getLength() <em>Length</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLength()
	 * @generated
	 * @ordered
	 */
	protected static final int LENGTH_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getLength() <em>Length</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLength()
	 * @generated
	 * @ordered
	 */
	protected int length = LENGTH_EDEFAULT;

	/**
	 * The default value of the '{@link #getTotalNumberOfDataPoints() <em>Total Number Of Data Points</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTotalNumberOfDataPoints()
	 * @generated
	 * @ordered
	 */
	protected static final int TOTAL_NUMBER_OF_DATA_POINTS_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getTotalNumberOfDataPoints() <em>Total Number Of Data Points</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTotalNumberOfDataPoints()
	 * @generated
	 * @ordered
	 */
	protected int totalNumberOfDataPoints = TOTAL_NUMBER_OF_DATA_POINTS_EDEFAULT;

	/**
	 * The default value of the '{@link #getDataRepresentationTemplateNumber() <em>Data Representation Template Number</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataRepresentationTemplateNumber()
	 * @generated
	 * @ordered
	 */
	protected static final short DATA_REPRESENTATION_TEMPLATE_NUMBER_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getDataRepresentationTemplateNumber() <em>Data Representation Template Number</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataRepresentationTemplateNumber()
	 * @generated
	 * @ordered
	 */
	protected short dataRepresentationTemplateNumber = DATA_REPRESENTATION_TEMPLATE_NUMBER_EDEFAULT;

	/**
	 * The cached value of the '{@link #getDataRepresentationTemplate() <em>Data Representation Template</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataRepresentationTemplate()
	 * @generated
	 * @ordered
	 */
	protected IDataRepresentation dataRepresentationTemplate;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected DataRepresentationSectionImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SectionsPackage.Literals.DATA_REPRESENTATION_SECTION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public long getStartOffset() {
		return startOffset;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setStartOffset(long newStartOffset) {
		long oldStartOffset = startOffset;
		startOffset = newStartOffset;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.DATA_REPRESENTATION_SECTION__START_OFFSET, oldStartOffset, startOffset));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public long getEndOffset() {
		return endOffset;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setEndOffset(long newEndOffset) {
		long oldEndOffset = endOffset;
		endOffset = newEndOffset;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.DATA_REPRESENTATION_SECTION__END_OFFSET, oldEndOffset, endOffset));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getLength() {
		return length;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setLength(int newLength) {
		int oldLength = length;
		length = newLength;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.DATA_REPRESENTATION_SECTION__LENGTH, oldLength, length));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getTotalNumberOfDataPoints() {
		return totalNumberOfDataPoints;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setTotalNumberOfDataPoints(int newTotalNumberOfDataPoints) {
		int oldTotalNumberOfDataPoints = totalNumberOfDataPoints;
		totalNumberOfDataPoints = newTotalNumberOfDataPoints;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.DATA_REPRESENTATION_SECTION__TOTAL_NUMBER_OF_DATA_POINTS, oldTotalNumberOfDataPoints, totalNumberOfDataPoints));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public short getDataRepresentationTemplateNumber() {
		return dataRepresentationTemplateNumber;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setDataRepresentationTemplateNumber(short newDataRepresentationTemplateNumber) {
		short oldDataRepresentationTemplateNumber = dataRepresentationTemplateNumber;
		dataRepresentationTemplateNumber = newDataRepresentationTemplateNumber;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE_NUMBER, oldDataRepresentationTemplateNumber, dataRepresentationTemplateNumber));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public IDataRepresentation getDataRepresentationTemplate() {
		return dataRepresentationTemplate;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetDataRepresentationTemplate(IDataRepresentation newDataRepresentationTemplate, NotificationChain msgs) {
		IDataRepresentation oldDataRepresentationTemplate = dataRepresentationTemplate;
		dataRepresentationTemplate = newDataRepresentationTemplate;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE, oldDataRepresentationTemplate, newDataRepresentationTemplate);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setDataRepresentationTemplate(IDataRepresentation newDataRepresentationTemplate) {
		if (newDataRepresentationTemplate != dataRepresentationTemplate) {
			NotificationChain msgs = null;
			if (dataRepresentationTemplate != null)
				msgs = ((InternalEObject)dataRepresentationTemplate).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE, null, msgs);
			if (newDataRepresentationTemplate != null)
				msgs = ((InternalEObject)newDataRepresentationTemplate).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE, null, msgs);
			msgs = basicSetDataRepresentationTemplate(newDataRepresentationTemplate, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE, newDataRepresentationTemplate, newDataRepresentationTemplate));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE:
				return basicSetDataRepresentationTemplate(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SectionsPackage.DATA_REPRESENTATION_SECTION__START_OFFSET:
				return getStartOffset();
			case SectionsPackage.DATA_REPRESENTATION_SECTION__END_OFFSET:
				return getEndOffset();
			case SectionsPackage.DATA_REPRESENTATION_SECTION__LENGTH:
				return getLength();
			case SectionsPackage.DATA_REPRESENTATION_SECTION__TOTAL_NUMBER_OF_DATA_POINTS:
				return getTotalNumberOfDataPoints();
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE_NUMBER:
				return getDataRepresentationTemplateNumber();
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE:
				return getDataRepresentationTemplate();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case SectionsPackage.DATA_REPRESENTATION_SECTION__START_OFFSET:
				setStartOffset((Long)newValue);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__END_OFFSET:
				setEndOffset((Long)newValue);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__LENGTH:
				setLength((Integer)newValue);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__TOTAL_NUMBER_OF_DATA_POINTS:
				setTotalNumberOfDataPoints((Integer)newValue);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE_NUMBER:
				setDataRepresentationTemplateNumber((Short)newValue);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE:
				setDataRepresentationTemplate((IDataRepresentation)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case SectionsPackage.DATA_REPRESENTATION_SECTION__START_OFFSET:
				setStartOffset(START_OFFSET_EDEFAULT);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__END_OFFSET:
				setEndOffset(END_OFFSET_EDEFAULT);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__LENGTH:
				setLength(LENGTH_EDEFAULT);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__TOTAL_NUMBER_OF_DATA_POINTS:
				setTotalNumberOfDataPoints(TOTAL_NUMBER_OF_DATA_POINTS_EDEFAULT);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE_NUMBER:
				setDataRepresentationTemplateNumber(DATA_REPRESENTATION_TEMPLATE_NUMBER_EDEFAULT);
				return;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE:
				setDataRepresentationTemplate((IDataRepresentation)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SectionsPackage.DATA_REPRESENTATION_SECTION__START_OFFSET:
				return startOffset != START_OFFSET_EDEFAULT;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__END_OFFSET:
				return endOffset != END_OFFSET_EDEFAULT;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__LENGTH:
				return length != LENGTH_EDEFAULT;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__TOTAL_NUMBER_OF_DATA_POINTS:
				return totalNumberOfDataPoints != TOTAL_NUMBER_OF_DATA_POINTS_EDEFAULT;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE_NUMBER:
				return dataRepresentationTemplateNumber != DATA_REPRESENTATION_TEMPLATE_NUMBER_EDEFAULT;
			case SectionsPackage.DATA_REPRESENTATION_SECTION__DATA_REPRESENTATION_TEMPLATE:
				return dataRepresentationTemplate != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (startOffset: "); //$NON-NLS-1$
		result.append(startOffset);
		result.append(", endOffset: "); //$NON-NLS-1$
		result.append(endOffset);
		result.append(", length: "); //$NON-NLS-1$
		result.append(length);
		result.append(", totalNumberOfDataPoints: "); //$NON-NLS-1$
		result.append(totalNumberOfDataPoints);
		result.append(", dataRepresentationTemplateNumber: "); //$NON-NLS-1$
		result.append(dataRepresentationTemplateNumber);
		result.append(')');
		return result.toString();
	}

	/* (non-Javadoc)
	 * @see org.mro.grib.model.files.secondEdition.sections.DataRepresentationSection#initialise(java.nio.ByteBuffer)
	 */
	@Override
	public void initialise(ByteBuffer buffer) throws SectionException{
		
		totalNumberOfDataPoints = buffer.getInt();
		dataRepresentationTemplateNumber = buffer.getShort();
		
		try {
			dataRepresentationTemplate = GribSecondEditionTemplatesFactory.createGribSecondEdition(DataRepresentationTemplateNumber.get(dataRepresentationTemplateNumber));
		} catch (DataRepresentationException e) {
			throw new SectionException(e.getMessage());
		}
		dataRepresentationTemplate.initialise(buffer);
	}

} //DataRepresentationSectionImpl
